import time
import datetime


import pandas as pd

from transform_excel_tools.util import LogUtils

logger = LogUtils.get_logger("TransformData")


class TransformData(object):
    @staticmethod
    def transform_substation_data(file_list, output_file_dir):
        # 读取文件
        # TODO 去除
        logger.info("计算开始")
        time.sleep(5)
        file_dict = {file_list[i]['name']: file_list[i]['path'] for i in range(0, len(file_list))}
        df_dict = {}
        df_dict['1'] = df = pd.read_excel(io=file_dict["1"], sheet_name=1,
                                          dtype={"大类": str, "中类": str, "小类": str, "名称": str, "说明": str})
        # 转换文件
        for _, row in df.iterrows():
            if not isinstance(row["小类"], str):
                if not isinstance(row["中类"], str):
                    if isinstance(row["大类"], str):
                        row["小类"] = row["大类"] + "00"
                else:
                    row["小类"] = row["中类"] + "0"
            if isinstance(row["小类"], str) and len(row["小类"]) == 3:
                row["小类"] = "0" + row["小类"]
        df.dropna(axis=0, subset=["小类"], inplace=True)
        df = df[["小类", "名称"]]
        # 输出文件
        output_file = "{}/{}{}.xlsx".format(output_file_dir, "变电运检专业结果文件",
                                     datetime.datetime.now().strftime('%Y-%m-%d %H：%M：%S：%f'))
        df.to_excel(output_file, index=False, encoding="UTF8")
        return True, {"output_file": output_file, "output_file_dir": output_file_dir}
